package cn.java81.myschool.util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class MyBatisUtil2 {
    private static SqlSessionFactory sf;

    private MyBatisUtil2() {

    }

    static {
        try {
            InputStream inputStream = MyBatisUtil2.class.getClassLoader()
                    .getResourceAsStream("mybatis-config.xml");
            sf = new SqlSessionFactoryBuilder().build(inputStream);

        } catch (Exception e) {
            throw new RuntimeException("the Configuration of Mybatis is not exist!");
        }
    }

    public static SqlSession get() {
        return sf.openSession();
    }

    public static void close(SqlSession s) {
        if (s != null) {
            s.commit();
            s.close();
        }
    }

}

